package org.dl.gjz.menyj.controller;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/userTask")
public class UserTaskController {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private IdentityService identityService;

    //
    //

    /**
     * 接口Restful启动：127.0.0.1:8088/server1/userTask/start/Process_vocation
     * processKey = Process_vocation
     * 启动购物流程实例
     * @param processKey
     */
    @GetMapping("/start/{processKey}")
    public Map<String, String> startProcess(@PathVariable(value = "processKey") String processKey) {
        //
        identityService.setAuthentication("xiaoming", null);
        // 流程启动
        Map<String, Object> variables = new HashMap<>();
        variables.put("approver", "xiaowang");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, variables);

        Map<String, String> result = new HashMap<>();
        result.put("processInstanceId", processInstance.getId());
        result.put("processDefinitionId", processInstance.getProcessDefinitionId());
        result.put("processDefinitionKey", processInstance.getProcessDefinitionKey());
        result.put("businessKey", processInstance.getBusinessKey());
        result.put("rootProcessInstanceId", processInstance.getRootProcessInstanceId());
        result.put("tenantId", processInstance.getTenantId());
        result.put("caseInstanceId", processInstance.getCaseInstanceId());
        result.put("isEnded", processInstance.isEnded() + "");
        result.put("isSuspended", processInstance.isSuspended() + "");
        log.info("启动流程实例：{}", result);
        /**
         * {"processInstanceId":"fca48db1-7d9f-11f0-9ca5-a002a5b68434",
         * "processDefinitionId":"Process_vocation:6:983baff7-7d8b-11f0-8ec4-a002a5b68434",
         * "isSuspended":"false","caseInstanceId":null,"businessKey":null,"tenantId":null,
         * "rootProcessInstanceId":"fca48db1-7d9f-11f0-9ca5-a002a5b68434",
         * "isEnded":"false",
         * "processDefinitionKey":"Process_vocation"}
         */
        return result;
    }
    // Process_vocation_script
    // 127.0.0.1:8088/server1/userTask/start2/Process_vocation_script
    @GetMapping("/start2/{processKey}")
    public String startProcess2(@PathVariable(value = "processKey") String processKey) {
        //
        identityService.setAuthentication("xiaoming", null);
        // 流程启动
        Map<String, Object> variables = new HashMap<>();
        variables.put("approver", "xiaowang");
        variables.put("originDays",15);
        runtimeService.startProcessInstanceByKey(processKey, variables);
        return "success";
    }
}
